<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head lang="en">
    <th:block th:include="include :: header('添加角色')"/>
</head>
<body>
<form class="layui-form" style="padding: 10px" lay-filter="form">

    <div class="layui-form-item">
        <label class="layui-form-label">主题</label>
        <div class="layui-input-block" id="theme">
        </div>
    </div>
    <input type="hidden" id="id" name="id">
    </div>
    <div style="display: none">
        <button class="layui-btn" lay-submit lay-filter="*" id="submit">立即提交</button>
    </div>
</form>
</body>
<th:block th:include="include :: footer"/>
<script type="text/javascript">

    var form;

    //从父层获取值，json是父层的全局js变量。eval是将该string类型的json串变为标准的json串
    layui.use(['form'], function () {
        form = layui.form;
        config.commGet("/ht/themeListSimple.json",function (data) {
            var html="";
            for (var i=0;i<data.length;i++){
                html+='<input type="checkbox" name="'+data[i].id+'" title="'+data[i].name+'">'
            }
            $("#theme").html(html);
            console.log(html)
            form.render()
        })
        if (parent.json) {
            var parent_json = parent.json;
            form.val("form", {
                "id": parent_json.id
            })
        }
        form.render("select");                                // 刷性select，显示出数据
        form.on('submit(*)', function (data) {
            // config.commPost("/ht/deviceAdd.json", data.field, function (data, info) {
            //     parent.config.tableRefresh();
            //     parent.config.closeLayer(info);
            // })
            var params={};
            params.id=data.field.id;
            var themes='';
            for(let key  in data.field){
               if(data.field[key]=='on'){
                   themes+=key+",";
               }
            }
            if(themes==''){
                config.alert("请选择主题")
                return  false;
            }
            themes=themes.substr(0,themes.length-1);
            console.log(themes)
            params.themes=themes;
            config.commPost("/ht/addDeviceTheme.json", params, function (data, info) {
                parent.config.tableRefresh();
                parent.config.closeLayer(info);
            })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    })

    function ApplicationSave() {
        document.getElementById("submit").click();
    }
</script>
</html>

